<template>
  <su-page bg="#f7f7f7">
    <su-body>
      <!-- header -->
      <su-white-header v-if="detailData.BT" :title="detailData.BT" :prop="detailData.FSSJ"></su-white-header>
      <!-- detail -->
      <su-prop-group title="通知详细信息" v-if="detailData">
        <su-prop label="发送人">{{detailData.FSR || ''}}</su-prop>
        <su-prop label="发送单位">{{detailData.FSDW || ''}}</su-prop>
        <su-prop label="附件">
        </su-prop>
      </su-prop-group>
			<view class="file">
				<view class="fileList" v-for="(item, index) in fileArr" :key="index">
						<!-- <u--image radius="6" :src="item.previewUrl" width="70" height="70" mode="aspectFill" @click="previewImg(index)"></u--image>	 -->
							
						<view @click="previewImg(index)" :key="index" v-if="item.FILENAME.indexOf('docx') != -1|| item.FILENAME.indexOf('doc') != -1" class="color-primary">{{item.FILENAME || ''}}</view>
						<image :src="item.previewUrl" class="imgStyle" v-if="item.FILENAME.indexOf('png') != -1"></image>
				</view>
			</view>
			<!-- 切换tab -->
			<u-subsection :list="list" :current="curNow" @change="sectionChange"></u-subsection>
      <!-- loading -->
      <su-list-loading :loading="detailLoading"></su-list-loading>
			<view class="company-list">
				<view class="company-list-item" v-for="(item, index) in companyList" :key="item.UUID">
					<view class="" @click="clickCompany(item, index)">
						{{item.DWMC}} - {{item.XMJL}}
					</view>
				</view>
			</view>
			<u-empty
				:show="showEmpty"
				mode="data"
				icon="http://cdn.uviewui.com/uview/empty/car.png"
			>
			</u-empty>
    </su-body>
		<u-toast ref="uToast"></u-toast>
  </su-page>
</template>

<script>
	/* @Description: 主管部门通知详情，分已回执未回执列表，点击跳转详情detailCompany.vue
	 * @Author: Yinchy
	 * @CreatDate: 2022-11-23 20:43:33
	 * @Params: 
	 */
	import { runSQL,fileIcon, previewPath } from '@/common/utils/wxutils'
  export default {
    data() {
      return {
        recid:0,
        detailData: null,
        detailLoading: false,
				fileArr: [],
				list: ['未反馈', '已反馈'],
				curNow: 0,
				companyList: [],
				pageSize: 20,
				pageNum: 0,
				showEmpty: true
      }
    },
    onLoad(params) {
			this.detailData = params || null;
			this.recid = params.RECID;
      this.getDetail()
    },
    methods: {
      // 获取详情
      getDetail() {
        this.detailLoading = true;
				let fileidArr = this.detailData.FJ ? this.detailData.FJ.split(',') : [this.detailData.FJ];
				fileidArr.forEach(item => {
					runSQL(`select * from ssnydw.tbfile where fileid = '${item}';`).then(res => {
						if(res[0].length > 0){
							res[0].forEach(file=>{
								if(!(/png|jpg|jpeg/).test(file.FILENAME)){
									file.previewUrl = fileIcon
								}else{
									file.previewUrl = previewPath + file.FILEID
								}
							})
							this.fileArr  = this.fileArr.concat(res[0][0]);
							console.log(this.fileArr);
						}
					})
				})
				this.getCompanyList()
				this.detailLoading = false;
      },
			previewImg(index){
				let _this = this;
				wx.downloadFile({
				  //文件url
				  url: previewPath + _this.fileArr[index].FILEID,
				  success: function (res) {
				    const filePath = res.tempFilePath
				    wx.openDocument({
				      filePath: filePath,
							fileType: _this.fileArr[index].STOREFILENAME.substring(_this.fileArr[index].STOREFILENAME.lastIndexOf(".")+1),
				      success: function (res) {
				        console.log('打开文档成功')
				      },
				      fail: function (res) {
				        console.log(res,'打开文档失败')
				      },
				    })
				  }
				})
			},
			sectionChange(curNow){
				this.curNow = curNow;
				this.pageNum = 0;
				this.companyList = []
				this.getCompanyList()
			},
			scrolltolower(){
				console.log('cd');
			},
			onReachBottom() {
			  this.getCompanyList()
			},
			getCompanyList(){
				runSQL(`select * from ssnydw.tzfsfb where recid = ${this.recid} and zt='${this.curNow}' limit ${this.pageSize} OFFSET ${this.pageNum * this.pageSize};`).then(res=>{
					if(res[0].length > 0){
						if(res[0].length < this.pageSize){
							this.companyList = this.companyList.concat(res[0]);
						}else{
							this.companyList = this.companyList.concat(res[0]);
							this.pageNum++;
						}
						this.showEmpty = false;
					}else{
						this.showEmpty = true;
					}
				})
			},
			clickCompany(item, index){
				this.$u.route({
					url: '/pages/main/notice/detail/detailCompany',
					params: {
						...item,
						...this.detailData,
						curNow: this.curNow//判断点击的是已回执1还是未回执0
					}
				})
			}
    },
  }
</script>

<style lang="scss" scoped>
	.file {
		display: flex;
		flex-wrap: wrap;
		background-color: #fff;
		padding: 20upx;
		margin-right: 20upx;
	}
	
	.company-list{
		padding: 0 20upx;
		&-item{
			min-height: 2rem;
			line-height: 2rem;
			background-color: #fff;
			margin-top: 4upx;
			padding: 0 4upx;
		}
	}
</style>
